<template>
  <div class="dashboard">
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside class="sidebar" :width="sidebarWidth">
        <div class="sidebar-header">
          <h2>管理端</h2>
        </div>
        
        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          @select="handleMenuSelect"
        >
          
          <el-menu-item index="contacts">
            <el-icon><User /></el-icon>
            <span>联系人管理</span>
          </el-menu-item>
           <el-menu-item index="channelDistributor">
            <el-icon><Setting /></el-icon>
            <span>渠道管理</span>
          </el-menu-item>
          <el-menu-item index="access-record">
            <el-icon><View /></el-icon>
            <span>访问记录</span>
          </el-menu-item>
          <el-menu-item index="system">
            <el-icon><Setting /></el-icon>
            <span>系统管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 主内容区域 -->
      <el-container>
        <el-header class="header">
          <div class="header-left">
            <el-button 
              type="text" 
              @click="toggleSidebar"
              class="sidebar-toggle"
            >
              <el-icon><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
            </el-button>
            <h3>issue群组管理系统</h3>
          </div>
          <div class="header-right">
            <span>欢迎，{{ userInfo.username }}</span>
            <el-button type="danger" size="small" @click="handleLogout">
              退出登录
            </el-button>
          </div>
        </el-header>
        
        <el-main class="main-content">
          <!-- 根据当前路由显示不同内容 -->
          
          <div v-if="currentView === 'contacts'">
            <Contacts />
          </div>
          <div v-else-if="currentView === 'system'">
            <System />
          </div>
          <div v-else-if="currentView === 'access-record'">
            <VisitRecord />
          </div>
          <div v-else-if="currentView === 'channelDistributor'">
            <ChannelDistributor />
          </div>
          <div v-else class="welcome-content">
            <el-card>
              <template #header>
                <div class="card-header">
                  <span>系统概览</span>
                </div>
              </template>
              <div class="card-content">
                
                <!-- <el-divider /> -->
                <p>请选择左侧菜单开始使用：</p>
                <ul>
                  <li>👤 联系人编辑 - 管理联系人信息</li>
                  <li>⚙️ 系统管理 - 系统配置和管理</li>
                </ul>
              </div>
            </el-card>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { DataAnalysis, User, Setting, Expand, Fold } from '@element-plus/icons-vue'
import { logout } from '@/api/user.js'
import Contacts from './Contacts.vue'
import System from './System.vue'
import VisitRecord from './VisitRecord.vue'
import ChannelDistributor from './ChannelDistributor.vue'



export default {
  name: 'Dashboard',
  components: {
    DataAnalysis,
    User,
    Setting,
    Expand,
    Fold,
    Contacts,
    System,
    VisitRecord,
    ChannelDistributor
  },
  setup() {
    const router = useRouter()
    const route = useRoute()
    const userInfo = ref({})
    const isCollapse = ref(false)
    
    // 计算侧边栏宽度
    const sidebarWidth = computed(() => isCollapse.value ? '64px' : '200px')
    
    // 当前激活的菜单
    const activeMenu = ref('')
    
    // 当前视图
    const currentView = ref('welcome')
    
    onMounted(() => {
      const storedUserInfo = localStorage.getItem('userInfo')
      if (storedUserInfo) {
        userInfo.value = JSON.parse(storedUserInfo)
      }
      
      // 根据当前路由设置激活菜单
      if (route.path === '/groups') {
        activeMenu.value = 'groups'
        currentView.value = 'groups'
      } else if (route.path === '/contacts') {
        activeMenu.value = 'contacts'
        currentView.value = 'contacts'
      } else if (route.path === '/system') {
        activeMenu.value = 'system'
        currentView.value = 'system'
      }else if (route.path === '/visitRecord') {
        activeMenu.value = 'visitRecord'
        currentView.value = 'visitRecord'
      } else if (route.path === '/channelDistributor') {
        activeMenu.value = 'channelDistributor'
        currentView.value = 'channelDistributor'
      }
    })
    
    const formatTime = (timeString) => {
      return new Date(timeString).toLocaleString('zh-CN')
    }
    
    // 切换侧边栏
    const toggleSidebar = () => {
      isCollapse.value = !isCollapse.value
    }
    
    // 菜单选择处理
    const handleMenuSelect = (index) => {
      activeMenu.value = index
      currentView.value = index
    }
    
    const handleLogout = async () => {
      try {
        await ElMessageBox.confirm(
          '确定要退出登录吗？',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        
        try {
          // 调用退出登录API
          await logout()
        } catch (error) {
          console.warn('Logout API call failed:', error)
          // 即使API调用失败，也要清除本地数据
        }
        
        // 清除本地存储
        localStorage.removeItem('token')
        localStorage.removeItem('userInfo')
        ElMessage.success('已退出登录')
        router.push('/login')
      } catch {
        // 用户取消退出
      }
    }
    
    return {
      userInfo,
      isCollapse,
      sidebarWidth,
      activeMenu,
      currentView,
      formatTime,
      toggleSidebar,
      handleMenuSelect,
      handleLogout
    }
  }
}
</script>

<style scoped>
.dashboard {
  height: 100vh;
}

/* 侧边栏样式 */
.sidebar {
  background: #2c3e50;
  transition: width 0.3s ease;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #34495e;
}

.sidebar-header h2 {
  color: #fff;
  font-size: 18px;
  font-weight: 600;
  margin: 0;
}

.sidebar-menu {
  border: none;
  background: transparent;
  width: 100%;
}

.sidebar-menu .el-menu-item {
  color: #bdc3c7;
  border-radius: 0;
  margin: 4px 8px;
  width: calc(100% - 16px);
  transition: all 0.3s ease;
}

.sidebar-menu .el-menu-item:hover {
  background: #34495e;
  color: #fff;
}

.sidebar-menu .el-menu-item.is-active {
  background: #3498db;
  color: #fff;
}

.sidebar-menu .el-menu-item span {
  margin-left: 8px;
}

/* 头部样式 */
.header {
  background: #fff;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 15px;
  height: 100%;
}

.sidebar-toggle {
  font-size: 18px;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0;
  border: none;
  background: none;
}

.header-left h3 {
  color: #333;
  margin: 0;
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.header-right span {
  color: #666;
  font-size: 14px;
}

/* 主内容区域 */
.main-content {
  background: #f5f5f5;
  padding: 0;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

/* 欢迎页面样式 */
.welcome-content {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  font-weight: 600;
  font-size: 16px;
}

.card-content {
  line-height: 1.6;
}

.card-content p {
  margin-bottom: 10px;
  color: #333;
}

.card-content ul {
  margin-top: 15px;
  padding-left: 20px;
}

.card-content li {
  margin-bottom: 8px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: absolute;
    z-index: 1000;
    height: 100vh;
  }
  
  .sidebar:not(.el-aside--collapsed) {
    width: 200px !important;
  }
}
</style>
